<?php 
class Pager
{
	public $ItemsTotal;
	public $ItemsPerPage;
	
	public $PageCurrent;
	public $PagesCount;
	
	public function Pager($ipp = 0)
	{
		self::setItemsTotal(0);
		if(intval($ipp) > 0)
		{ self::setItemsPerPage(intval($ipp)); }
		else
		{ 
			if(intval($_REQUEST['ipp']) > 0)
			{ self::setItemsPerPage(intval($_REQUEST['ipp'])); }
			else
			{ self::setItemsPerPage(15); }
		}
		self::setPagesCount(0);
		if((intval($_REQUEST['page']) > 0) && (intval($_REQUEST['page']) < 1000000))
		{ self::setPageCurrent(intval($_REQUEST['page'])); }
		else
		{ self::setPageCurrent(1); }
	}
	
	public function getItemsTotal() { return intval($this->ItemsTotal); }
	public function getItemsPerPage() { return intval($this->ItemsPerPage); }
	public function getPageCurrent() { return intval($this->PageCurrent); }
	public function getPagesCount() { self::calcPagesCount(); return intval($this->PagesCount); }
	
	public function setItemsTotal($val) { $this->ItemsTotal=intval($val); self::calcPagesCount(); }
	public function setItemsPerPage($val) { $this->ItemsPerPage=intval($val); }
	public function setPageCurrent($val) { $this->PageCurrent=intval($val); }
	public function setPagesCount($val) { $this->PagesCount=intval($val); }
	
	public function calcPagesCount()
	{
		if((self::getItemsTotal() > 0) && (self::getItemsPerPage() > 0))
		{ self::setPagesCount(ceil(self::getItemsTotal() / self::getItemsPerPage())); }
		else
		{ 
			self::setPagesCount(0); 
			self::setPageCurrent(1);
		}
	}
	
	public function getLimitStart()
	{
		$LimitStart = 0;
		if((self::getPageCurrent() > 0) && (self::getItemsPerPage() > 0))
		{ $LimitStart = (self::getPageCurrent() - 1) * self::getItemsPerPage(); }
		return $LimitStart;
	}
	
	public function getHTML($display = 6, $formId = 'search-ad-form', $pageInputId = 'pageNo')
	{
		if(self::getPagesCount() > 1)
		{
			global $txt;
			$params = '';
			
			$pagerHTML  = '<div class="pagination">';
			$pagerHTML  .= 	'<ul>';
			
			if((self::getPagesCount() > 0) && (self::getPageCurrent() > 1))
			{ $disabled = ''; }
			else
			{ $disabled = 'disabled'; }
			
			$pagerHTML .= '<li class="'.$disabled.'"><a class="command" onclick="$(\'#'.$pageInputId.'\').val('.(self::getPageCurrent() - 1).'); $(\'#'.$formId.'\').submit();" title="'.ucfirst($txt->getWord('previous')).'">&laquo;</a></li>'; 
			
			if($display > self::getPagesCount())
			{ $display = self::getPagesCount(); }
			
			$displayed = 0;
			
			$pagerBeforeHTML = '';
			$pagerAfterHTML = '';
				
			for($i=1; $i <= $display; $i++)
			{
				$before = self::getPageCurrent() - $i;
				$after  = self::getPageCurrent() + $i;
				
				if($displayed < $display)
				{
					if($before > 0)
					{ 
						$pagerBeforeHTML = '<li><a class="command" onclick="$(\'#'.$pageInputId.'\').val('.$before.'); $(\'#'.$formId.'\').submit();" title="'.$before.'">'.$before.'</a></li>'.$pagerBeforeHTML; 
						$displayed++;
						
					}
					if(($after > self::getPageCurrent()) && ($after <= self::getPagesCount()))
					{
						$pagerAfterHTML .= '<li><a class="command" onclick="$(\'#'.$pageInputId.'\').val('.$after.'); $(\'#'.$formId.'\').submit();" title="'.$after.'">'.$after.'</a></li>';
						$displayed++;
					}
				}
			}
	
			$pagerHTML .= $pagerBeforeHTML.'<li class="active"><a class="command">'.self::getPageCurrent().'</a></li>'.$pagerAfterHTML;
			
			if((self::getPagesCount() > 0) && (self::getPageCurrent() < self::getPagesCount()))
			{ $pagerHTML .= '<li><a class="command" onclick="$(\'#'.$pageInputId.'\').val('.(self::getPageCurrent() + 1).'); $(\'#'.$formId.'\').submit();" title="'.ucfirst($txt->getWord('next')).'">&raquo;</a></li>'; }
			$pagerHTML  .= 	'</ul>';
			$pagerHTML .= '</div>';
			return $pagerHTML;
		}
	}
	
	public function getLinksHTML($display = 8)
	{
		if(self::getPagesCount() > 1)
		{
			global $txt;
			$params = '';
			
			$pagerHTML  = '<ul>';
			
			if((self::getPagesCount() > 0) && (self::getPageCurrent() > 1))
			{ $pagerHTML .= '<li><a href="/page/'.(self::getPageCurrent() - 1).'/" title="'.ucfirst($txt->getWord('previous')).'">&lt;&lt;</a></li>'; }
			else
			{ $pagerHTML .= '<li class="disabled"><a href="#">&lt;&lt;</a></li>'; }
			
			if($display > self::getPagesCount())
			{ $display = self::getPagesCount(); }
			
			$displayed = 0;
			
			$pagerBeforeHTML = '';
			$pagerAfterHTML = '';
				
			for($i=1; $i <= $display; $i++)
			{
				$before = self::getPageCurrent() - $i;
				$after  = self::getPageCurrent() + $i;
				
				if($displayed < $display)
				{
					if($before > 0)
					{ 
						$pagerBeforeHTML = '<li><a href="/page/'.$before.'/" title="'.$before.'">'.$before.'</a></li>'.$pagerBeforeHTML; 
						$displayed++;
						
					}
					if(($after > self::getPageCurrent()) && ($after <= self::getPagesCount()))
					{
						$pagerAfterHTML .= '<li><a href="/page/'.$after.'/" title="'.$after.'">'.$after.'</a></li>';
						$displayed++;
					}
				}
			}
	
			$pagerHTML .= $pagerBeforeHTML.'<li class="active"><a>'.self::getPageCurrent().'</a></li>'.$pagerAfterHTML;
			
			if((self::getPagesCount() > 0) && (self::getPageCurrent() < self::getPagesCount()))
			{ $pagerHTML .= '<li><a class="command" href="/page/'.(self::getPageCurrent() + 1).'/" title="'.ucfirst($txt->getWord('next')).'">&gt;&gt;</a></li>'; }
				
			$pagerHTML .= '</ul>';
			return $pagerHTML;
		}
	}
}
?>